#ifndef __SINGLETON_HH__
#define __SINGLETON_HH__

#include <boost/scoped_ptr.hpp>
#include <boost/thread/once.hpp>
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <boost/utility.hpp>
#include <cassert>

namespace Common
{

/**
 * @class Singleton
 * @brief Singleton pattern implementation.
 */
template< typename T >
class Singleton : public boost::noncopyable
{
protected:
    /**
     * @brief Singleton object default ctor.
     */
    Singleton( void )
    {
    }

    /**
     * @brief Singleton default dtor
     */
    virtual ~Singleton( void )
    {        
    }
            
public:

    /**
     * @brief
     */
    static T* instance( void )
    {
        boost::call_once( init, iOnceFlag );
        return iObjPtr.get();
    }
    
private:
    template< typename... TArgs >
    static void init( const TArgs&... args )
    {
        iObjPtr.reset( new T( args... ) );
    }
    
private:
    ///pointer to derived object
    static boost::scoped_ptr< T > iObjPtr;
    static boost::once_flag       iOnceFlag;
    
};

template< typename T >
boost::scoped_ptr< T > Singleton< T >::iObjPtr;

template< typename T >
boost::once_flag Singleton< T >::iOnceFlag = BOOST_ONCE_INIT;

} // namespace Common

#endif //__SINGLETON_HH__
